package com.tuzhi.base.redis;

import org.apache.commons.lang3.ArrayUtils;

/**
 * 通用业务key
 */
public class MyRedisKeyUtil {

    public final static String SEPARATE_CHAR = ":";
    public final static String CONFIG = "config";
    public final static String TEMP = "temp";

    public static String configKey(String ...strings) {
        return buildKey(CONFIG, strings);
    }


    public static String tempKey(String ...strings) {
        return buildKey(TEMP, strings);
    }

    public static String buildKey(String type, String ...strings) {
        checkPar(strings);
        String rs = type;
        for (String string : strings) {
            rs += SEPARATE_CHAR + string;
        }
        return rs;
    }


    private static void checkPar(String ...strings) {
        if (ArrayUtils.isEmpty(strings)) {
            throw new RuntimeException("传入参数不能为空！");
        }
        if (strings.length > 5) {
            throw new RuntimeException("key的拼接长度不能超过5个！");
        }
    }

    public static void main(String[] args) {
        System.out.println(configKey("aaa", "bbb"));
        System.out.println(tempKey("11", "bbb", "aaaa"));
        System.out.println(tempKey("11", "bbb", "aaaa", "", "", ""));
    }

}
